﻿var lastNoteElem = null;
var lastNoteElemBg = null;
var noteOperation = "";

function loadLastNoteElements() 
{
    var elem = document.getElementById(lastNoteElem);
    var elemChild = elem.getElementsByTagName("td");
    document.getElementById('txtTitle').value = elemChild[0].innerHTML;
    document.getElementById('txtDescription').value = elemChild[1].innerHTML;        
}

function selectNoteTableElement(elemID) 
{
    if (lastNoteElem != null) {
        document.getElementById(lastNoteElem).style.backgroundColor = lastNoteElemBg;
    }
    var elem = document.getElementById(elemID);
    lastNoteElem = elemID;
    lastNoteElemBg = elem.style.backgroundColor;
    elem.style.backgroundColor = "#FEF0C9";
}

function showNotePopup(operation) 
{
    if (operation == "Add") {
        document.getElementById('btnNoteOperation').value = "Add Note";
        document.getElementById('tdOperation').innerHTML = "Add Note";
        clearNotePopUpElements();
    }
    else
    if (operation == "Edit") 
    {
        if (lastNoteElem == null) 
            alert('No Note Selected');
        loadLastNoteElements();
        document.getElementById('btnNoteOperation').value = "Save Changes";
        document.getElementById('tdOperation').innerHTML = "Edit Note";
    }
    document.getElementById('lblErrorMessage').innerHTML = '';
    document.getElementById('divNotePopup').style.display = '';
    relocateNotePopup();
    document.getElementById('divNotes').style.opacity = 0.5;
    noteOperation = operation;

    toggleNoteButtons(false);
}

function clearNotePopUpElements() 
{
    document.getElementById('txtTitle').value = '';
    document.getElementById('txtDescription').value = '';
}

function relocateNotePopup() 
{
    var popup = document.getElementById("divNotePopup")
    if (popup.style.display != 'none') {
        var width = getViewPortWidth();
        width -= 500;
        width -= popup.style.width.replace("px", "");
        popup.style.left = width / 2 + 'px';
        popup.style.top = "100px";
    }
}

function toggleNoteButtons(enable) 
{
    document.getElementById('btnAddNote').disabled = !enable;
    document.getElementById('btnEditNote').disabled = !enable;
    document.getElementById('btnDeleteNote').disabled = !enable;
}

function CancelNoteOperation() 
{
    document.getElementById('divNotePopup').style.display = 'none';
    document.getElementById('divNotes').style.opacity = 1.0;
    toggleNoteButtons(true);
}

function PerformNoteOperation() 
{
    var title = document.getElementById('txtTitle').value;
    var desc = document.getElementById('txtDescription').value;
    var userName = document.getElementById('txtCurrentUser').value;
    
    errMsg = "";
    if (title == "") {
        errMsg += "Title is Required";
    }

    if (errMsg != "") {
        document.getElementById('lblErrorMessage').innerHTML = errMsg;
        return;
    }

    var url = "Personal/Notes.aspx";

    if (noteOperation == "Add") {
        url = url + "?insertnote=1";
        url = url + "&username=" + URLEncode(userName);
    }
    else
    if (noteOperation == "Edit") 
    {
        url = url + "?updatenote=1";
        try 
        {
            noteID = document.getElementById(lastNoteElem).getAttribute("noteID");
        }
        catch (ex) 
        {
            alert('No Note Selected');
            return;
        }
        url = url + "&noteID=" + URLEncode(noteID);
    }


    url = url + "&title=" + URLEncode(title);
    url = url + "&desc=" + URLEncode(desc);
    url = url + "&sid=" + Math.random();

    //alert(url);
    response = SubmitNoteForm(url);
    //alert(response);
    if (response.indexOf("Success") > -1) {
        loadXMLDoc('Personal/Notes.aspx');
        if (lastNoteElem != null) {
            document.getElementById(lastNoteElem).style.backgroundColor = "#FEF0C9";
        }
        if (noteOperation == "Add") {
            alert("New Note Successfully Added");
        }
        else if (noteOperation == "Edit") {
            alert("Note Successfully Updated");
        }
    } 
    else {
        document.getElementById('lblErrorMessage').innerHTML = response;
       // alert(response);
    }

    return false;
}

function deleteSelectedNote() 
{
    if (lastNoteElem == null) {
        alert('No Note Selected');
    }
    else {
        var result = confirm("Are you sure you want to delete this note?");

        if (result == 1) {
            var noteID = document.getElementById(lastNoteElem).getAttribute("noteID");
            var url = "Personal/Notes.aspx";
            url = url + "?deletenote=1";
            url = url + "&noteID=" + URLEncode(noteID);
            url = url + "&sid=" + Math.random();

            //alert(url);

            response = SubmitNoteForm(url);

            if (response.indexOf("Success") > -1) {
                loadXMLDoc('Personal/Notes.aspx');
                alert("Note Successfully Deleted");
            }
            else {
                alert("There was a problem deleting the note");
            }
        }
    }
}

function SubmitNoteForm(url)
{
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET", url, false);
    xmlhttp.send(null);
    response = xmlhttp.responseText;
    //alert(response);
    //alert(response.substring(response.indexOf("Line"),response.length));
    response = response.substring(response.indexOf("<response>"), response.indexOf("</response"));
    //alert(response);

    return response;
}



